import 'package:flutter/material.dart';

class NoteHomePage extends StatefulWidget {
  const NoteHomePage({super.key});

  @override
  State<NoteHomePage> createState() => _NoteHomePageState();
}

class _NoteHomePageState extends State<NoteHomePage> {
  // 状态变量
  final List<String> directories = ['工作笔记', '个人日记', '学习资料', '项目计划'];
  final Map<String, List<String>> files = {
    '工作笔记': ['周报2023-10-01', '会议记录', 'TODO列表'],
    '个人日记': ['2023-10-01', '旅行日记', '读书笔记'],
    '学习资料': ['Flutter学习', 'Dart语法', 'UI设计'],
    '项目计划': ['里程碑1', '需求分析', '开发计划'],
  };

  String selectedDirectory = '工作笔记';
  String selectedFile = '周报2023-10-01';
  final TextEditingController _textController = TextEditingController();

  // 可拖动宽度控制
  double directoryWidth = 150.0;
  double fileWidth = 150.0;

  @override
  void initState() {
    super.initState();
    _textController.text = '这里是文本编辑区域。\n\n您可以在这里输入笔记内容...';
  }

  @override
  void dispose() {
    _textController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Note App'),
        actions: [
          IconButton(icon: const Icon(Icons.search), onPressed: () {}),
          IconButton(icon: const Icon(Icons.more_vert), onPressed: () {}),
        ],
      ),
      body: _buildMainLayout(),
    );
  }

  // 核心布局方法（优化后的部分）
  Widget _buildMainLayout() {
    return Row(
      children: [
        // 左侧可拖动两列
        _buildLeftPanel(),

        // 分隔线
        const VerticalDivider(width: 1),

        // 右侧自适应编辑区
        Expanded(
          child: Column(
            children: [
              // 工具栏
              Container(
                height: 40,
                color: Colors.grey[50],
                child: Row(/* 工具栏按钮... */),
              ),
              // 文本编辑区
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: TextField(
                    controller: _textController,
                    maxLines: null,  // 允许多行
                    expands: true,   // 填满可用空间
                    decoration: const InputDecoration(
                      border: InputBorder.none, // 无边框
                      hintText: '输入笔记内容...',
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ],
    );
  }

  // 左侧面板组件
  Widget _buildLeftPanel() {
    return SizedBox(
      width: directoryWidth + fileWidth + 2,
      child: Row(
        children: [
          // 目录列
          _buildResizableColumn(
            width: directoryWidth,
            child: ListView.builder(
              itemCount: directories.length,
              itemBuilder: (ctx, index) => ListTile(
                title: Text(directories[index]),
                selected: directories[index] == selectedDirectory,
                onTap: () => setState(() {
                  selectedDirectory = directories[index];
                  selectedFile = files[directories[index]]?.first ?? '';
                }),
              ),
            ),
            onResize: (delta) => setState(() => directoryWidth += delta),
          ),

          // 文件列
          _buildResizableColumn(
            width: fileWidth,
            child: ListView.builder(
              itemCount: files[selectedDirectory]?.length ?? 0,
              itemBuilder: (ctx, index) => ListTile(
                title: Text(files[selectedDirectory]![index]),
                selected: files[selectedDirectory]![index] == selectedFile,
                onTap: () => setState(() => selectedFile = files[selectedDirectory]![index]),
              ),
            ),
            onResize: (delta) => setState(() => fileWidth += delta),
          ),
        ],
      ),
    );
  }

  // 可拖动列通用组件
  Widget _buildResizableColumn({
    required double width,
    required Widget child,
    required Function(double) onResize,
  }) {
    return Stack(
      children: [
        SizedBox(width: width, child: child),
        // 右侧拖动条
        Positioned(
          right: 0,
          top: 0,
          bottom: 0,
          child: MouseRegion(
            cursor: SystemMouseCursors.resizeColumn,
            child: GestureDetector(
              behavior: HitTestBehavior.translucent,
              onPanUpdate: (details) {
                final newWidth = width + details.delta.dx;
                if (newWidth > 100) { // 限制最小宽度
                  onResize(details.delta.dx);
                }
              },
              child: Container(
                width: 4,
                color: Colors.blue.withOpacity(0.3),
              ),
            ),
          ),
        ),
      ],
    );
  }
}